package com.thread.example.mythread;

import java.util.concurrent.CountDownLatch;

public class MyCountDownLatchDemo {

    private static CountDownLatch countDownLatch1 = new CountDownLatch(1);
    static Thread thread1 = new Thread(() -> {
        System.out.println("开始运行thread1");
        System.out.println("thread1------");
        countDownLatch1.countDown();
    });
    private static CountDownLatch countDownLatch2 = new CountDownLatch(1);
    static Thread thread2 = new Thread(() -> {
        System.out.println("开始运行thread2");
        try {
            countDownLatch1.await();
            System.out.println("thread2------");
            countDownLatch2.countDown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    });

    static Thread thread3 = new Thread(() -> {
        System.out.println("开始运行thread3");
        try {
            countDownLatch2.await();
            System.out.println("thread3------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    });

    public static void main(String[] args) throws InterruptedException {
        thread3.start();
        thread1.start();
        thread2.start();
        System.out.println("执行完毕");
    }
}
